home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swaga_c.zip / CMDLINE.SWG / 0010_Parsing a String.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  2KB  |  66 lines

  1. {
  2. From: BRIAN PAPE
  3. Subj: Reading from strings (PARSE)
  4. ---------------------------------------------------------------------------
  5. Can anyone out there tell me how to read a portion of one string
  6. variable into another?
  7.  
  8. I grabbed this code from one of my batch file utilities.  It isn't real
  9. efficient, as it uses COPY and DELETE, but it doesn't have to be, since
  10. it is only called once a program.  Where I have cfg:cfgrec, just replace
  11. cfgrec with your own kind of record. }
  12.  
  13. type
  14.   MyRec = record
  15.     r_var : integer;
  16.   end;
  17. ...
  18. Anyway, the GET function is what parses the /x:xxxx stuff out of a
  19. regular string.  The PARSE procedure gets the actual command tail
  20. from the PSP and keeps GETting parameters from it until it is empty.
  21. BTW, the atoi() function I used is merely val() turned into a function.
  22. ----------------------------------
  23. procedure parse(var cfg:cfgrec);
  24. function get(var s:string):string;
  25. var i,j : byte;slashflag : boolean;
  26. begin
  27.   i := 1;
  28.   while (s[i] = ' ') and (i<=length(s)) do inc(i);
  29.   slashflag := s[i] in ['-','/'];
  30.   j := succ(i);
  31.   while ((slashflag and not (s[j] in ['-','/'])) or
  32.         (not slashflag and not (s[j] = ' '))) and
  33.         (j<=length(s)) do inc(j);
  34.   get := copy(s,i,j-i);
  35.   delete(s,1,j-1);
  36. end;  { get }
  37.  
  38. var s:^string;t:string;
  39. begin
  40.   s := ptr(prefixseg,$80);  { DTA from PSP }
  41.   cfg.working_msg := '';
  42.   cfg.error_msg := '';
  43.   cfg.drive := 0;
  44.   cfg.pause_on_error := false;
  45.   cfg.how_many_retries := 1;
  46.   while s^<>'' do
  47.     begin
  48.       t := get(s^);
  49.       if t[1] in ['-','/'] then
  50.         begin
  51.           if length(t)>=2 then
  52.             case upcase(t[2]) of
  53.               'C':cfg.how_many_retries :=
  54.                   atoi(strip(copy(t,4,length(t)-3),' '));
  55.               'H','?':begin writehelp; halt(0); end;
  56.               'W':cfg.working_msg := copy(t,4,length(t)-3);
  57.               'E':cfg.error_msg := copy(t,4,length(t)-3);
  58.               'P':cfg.pause_on_error := true;
  59.             end;  { case }
  60.         end  { if }
  61.       else
  62.         cfg.drive := ord(upcase(t[1]))-65;
  63.     end;  { while }
  64. end;  { parse }
  65.  
  66.